package com.ilop.sthome.ui.adapter.device;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.utils.LiveDataBus;
import com.example.xmpic.support.FunSupport;
import com.example.xmpic.support.models.FunDevice;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019-10-11.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备展示
 */
public class DeviceChildAdapter extends RecyclerView.Adapter<DeviceChildAdapter.ItemHolder> {
    private static final String TAG = DeviceChildAdapter.class.getSimpleName();
    private Context mContext;
    private List<DeviceInfoBean> mList;
    private subDeviceCallBack mCallBack;

    public DeviceChildAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<DeviceInfoBean> deviceList) {
        this.mList = deviceList;
        notifyDataSetChanged();
    }

    public void setCallBack(subDeviceCallBack mCallBack) {
        this.mCallBack = mCallBack;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_device, viewGroup, false);
        return new ItemHolder(mView);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        DeviceInfoBean device = mList.get(i);
        if(device.getProductKey()!=null || "GATEWAY".equals(device.getNodeType())) {
            itemHolder.mIcon.setImageResource(R.mipmap.gs198);
            String gatewayName = mContext.getString(R.string.ali_gateway);
            itemHolder.mName.setText(TextUtils.isEmpty(device.getNickName()) ? gatewayName : device.getNickName());
            itemHolder.mType.setText(gatewayName);
            itemHolder.mStatus.setText(device.getStatus()==1?mContext.getString(R.string.on_line):mContext.getString(R.string.off_line));
            itemHolder.mStatus.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
            itemHolder.mColor.setBackgroundResource(device.getStatus()==1?R.drawable.device_status_normal:R.drawable.device_status_off_line);
            itemHolder.mGround.setBackgroundResource(device.getStatus()==1?R.drawable.round_corner_bg:R.drawable.round_gray_bg);
            itemHolder.mShare.setVisibility(device.getOwned() == 1 ? View.GONE : View.VISIBLE);
        }else if(device.getDevice_type()!= null || device.getDevice_ID()!=0){
            SmartDevice mSmart = SmartDevice.getType(device.getDevice_type());
            itemHolder.mIcon.setImageResource(mSmart.getDrawableResId());
            String deviceType = mContext.getString(mSmart.getTypeStrId());
            itemHolder.mName.setText(TextUtils.isEmpty(device.getSubdeviceName())?deviceType+device.getDevice_ID():device.getSubdeviceName());
            itemHolder.mType.setText(deviceType);

            int mStatusCode;
            if (mSmart == SmartDevice.EE_SIMULATE_IPC){
                FunDevice funDevice = FunSupport.getInstance().findDeviceBySn(device.getDeviceName());
                if (funDevice!=null){
                    itemHolder.mStatus.setText(funDevice.devStatus.getSatusId() == 1?mContext.getString(R.string.on_line):mContext.getString(R.string.off_line));
                    mStatusCode = funDevice.devStatus.getSatusId() == 1 ? 4 : 3;
                }else {
                    itemHolder.mStatus.setText(mContext.getString(R.string.on_line));
                    mStatusCode = 4;
                }
            } else if(mSmart == SmartDevice.EE_DEV_TH_CHECK){
                itemHolder.mStatus.setText(CoderUtils.getHumitureMsg(mContext, device.getDevice_type(), device.getDevice_status()));
                mStatusCode = DeviceStatusUtil.getStatus(device.getDevice_type(), device.getDevice_status());
            } else {
                String status = DeviceStatusUtil.getAlert(mContext, device.getDevice_type(), device.getDevice_status());
                itemHolder.mStatus.setText(status);
                mStatusCode = DeviceStatusUtil.getStatus(device.getDevice_type(), device.getDevice_status());
            }

            itemHolder.mColor.setBackgroundResource(ConfigureUtil.choseDeviceStateColor(mStatusCode));
            if (mStatusCode == 0){
                itemHolder.mStatus.setTextColor(ContextCompat.getColor(mContext, R.color.text_error));
                AnimationDrawable mAnimation = (AnimationDrawable) itemHolder.mColor.getBackground();
                mAnimation.start();
            }else {
                itemHolder.mStatus.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
            }
            itemHolder.mGround.setBackgroundResource(mStatusCode == 3?R.drawable.round_gray_bg:R.drawable.round_corner_bg);
            DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(device.getDeviceName());
            if (gateway!=null) {
                itemHolder.mShare.setVisibility(gateway.getOwned() == 1 ? View.GONE : View.VISIBLE);
            }else {
                itemHolder.mShare.setVisibility(View.GONE);
            }

            if (mSmart == SmartDevice.EE_DEV_UN_KNOW){
                DeviceDaoUtil.getInstance().getDeviceDao().delete(device);
            }
        }

        itemHolder.itemView.setOnClickListener(v -> {
            if (mCallBack == null){
                LiveDataBus.get().with(CommonId.DEVICE_ITEM_CLICK).setValue(device);
            }else {
                mCallBack.onClick(device);
            }
        });

        itemHolder.itemView.setOnTouchListener((v, event) -> {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    itemHolder.itemView.setScaleX(1.05f);
                    itemHolder.itemView.setScaleY(1.05f);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    itemHolder.itemView.setScaleX(1f);
                    itemHolder.itemView.setScaleY(1f);
                    break;
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_device_bg)
        LinearLayout mGround;
        @BindView(R.id.item_device_icon)
        ImageView mIcon;
        @BindView(R.id.item_device_name)
        TextView mName;
        @BindView(R.id.item_device_type)
        TextView mType;
        @BindView(R.id.item_device_status)
        TextView mStatus;
        @BindView(R.id.item_device_color)
        View mColor;
        @BindView(R.id.item_device_share)
        ImageView mShare;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface subDeviceCallBack{

        void onClick(DeviceInfoBean device);
    }
}
